import os
from lxml import etree
import csv


mapping = {
    "A+": 9,
    "A": 8,
    "A-": 7,
    "B+": 6,
    "B": 5,
    "B-": 4,
    "C+": 3,
    "C": 2,
    "C-": 1,
}


with open('resultset.csv', 'w', newline='', encoding='utf-8') as csvfile:
    spamwriter = csv.writer(csvfile,
                            delimiter='|',
                            quotechar='^',
                            quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(
        ("分类", "学科",  "大学",  "评级", "评价", "学科编号", "大学编号",))

    files = os.listdir("./pages")  # 得到文件夹下的所有文件名称

    for file in files:  # 遍历文件夹
        # 判断是否是文件夹，不是文件夹才打开
        file_title = file.split(".")[0]
        cata, sub_code, sub_name = file_title.split('-')

        with open("./pages/"+file, 'r', encoding='gb2312') as f:  # 打开文件
            ele = etree.HTML(f.read())
            table_ele = ele.xpath(
                '//table[@bgcolor="#c2d8e5"]/tbody/tr')
            current_ranking = ''
            for tr in table_ele:
                tds = tr.xpath("./td[@align='center']")
                if len(tds) > 1:
                    current_ranking = tds[0].text.strip()
                university = tr.xpath("./td[@align='center']/div")
                university_info = university[0].text.split()
                uni_code = university_info[0]
                uni_name = university_info[-1]

                ln = (cata, sub_name, uni_name, current_ranking,
                      mapping[current_ranking], sub_code, uni_code)
                spamwriter.writerow(ln)
                print(ln)
